package cn.iotab.iotkit.transport.api.access.model.custom.firmware;

import cn.iotab.iotkit.transport.api.access.model.message.CommonDeviceMessage;
import cn.iotab.iotkit.transport.api.access.model.message.OriginMessage;
import com.alibaba.fastjson.JSONObject;
import lombok.Getter;
import lombok.Setter;

/**
 * 上报固件更新进度
 *
 * 设备 向 平台 上报升级进度
 */
@Getter
@Setter
public class ReportUpgradeProgressMessage extends CommonDeviceMessage {

    /**
     * 进度0-100
     */
    private int progress;

    /**
     * 是否已完成
     */
    private boolean complete;

    /**
     * 升级中的固件版本
     */
    private String version;

    /**
     * 是否成功
     */
    private boolean success;

    /**
     * 错误原因
     */
    private String errorReason;

    /**
     * 固件ID
     *
     * @since 1.1.4
     */
    private String firmwareId;

    @Override
    public void fromOriginMessage(String productKey, String deviceId, OriginMessage message) {
        super.fromOriginMessage(productKey, deviceId, message);
        JSONObject jsonObject = getJsonParam();
        this.progress = jsonObject.getIntValue("progress");
        this.complete = jsonObject.getBooleanValue("complete");
        this.version = jsonObject.getString("version");
        this.success = jsonObject.getBooleanValue("success");
        this.errorReason = jsonObject.getString("errorReason");
        this.firmwareId = jsonObject.getString("firmwareId");
    }

    @Override
    public OriginMessage toOriginMessage() {
        throw new UnsupportedOperationException("unsupported operation for not support ReportUpgradeProgressMessage to device");
    }
}
